﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using CommandWebTestApp.PushService1;



namespace CommandWebTestApp
{
    public partial class Form1 : Form
    {
        PushServiceClient PushServiceClient_Temp = new PushServiceClient();

        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CreateCurrencyRateRequest CreateCurrencyRateRequestTemp = new CreateCurrencyRateRequest();
            CreateCurrencyRateRequestTemp.Value = Convert.ToInt32(textBox2.Text);
            CreateCurrencyRateRequestTemp.Period = Convert.ToDateTime(textBox3.Text);
            CreateCurrencyRateRequestTemp.CurrencyName = textBox4.Text;

            label2.Text = PushServiceClient_Temp.AddCurencyRate(CreateCurrencyRateRequestTemp).Status.ToString();
        }
        private void Timer_Tick()
        {
            WebRequest wReq = WebRequest.Create(@"http://www.bloomberg.com/markets/currencies/");
            WebResponse wResp = wReq.GetResponse();
            Stream respStream = wResp.GetResponseStream();
            StreamReader reader = new StreamReader(respStream, Encoding.ASCII);
            string respHTML = reader.ReadToEnd();

            int BeginPos = respHTML.IndexOf("<h2>Major World Currencies</h2>");
            int EndPos = respHTML.LastIndexOf("<div class='stock_chart'>") - BeginPos;
            respHTML = respHTML.Substring(BeginPos, EndPos);

            ParseItem(ref respHTML, "EUR-USD");
            ParseItem(ref respHTML, "GBP-USD");
            ParseItem(ref respHTML, "USD-JPY");
            ParseItem(ref respHTML, "AUD-USD");
            ParseItem(ref respHTML, "USD-CAD");
            ParseItem(ref respHTML, "USD-CHF");
            ParseItem(ref respHTML, "EUR-GBP");
            ParseItem(ref respHTML, "EUR-JPY");
            ParseItem(ref respHTML, "AUD-JPY");
        }
        private void ParseItem(ref string respHTML, string Currency)
        {
            //get value
            respHTML = respHTML.Substring(respHTML.IndexOf(Currency));
            respHTML = respHTML.Substring(8 + respHTML.IndexOf("'value'>"));
            string Value = respHTML.Substring(0, respHTML.IndexOf("</td>")).Replace('.', ',');

            //add to DB
            CreateCurrencyRateRequest CreateCurrencyRateRequest_Temp = new CreateCurrencyRateRequest();
            CreateCurrencyRateRequest_Temp.Period = DateTime.Now;
            CreateCurrencyRateRequest_Temp.CurrencyName = Currency;
            CreateCurrencyRateRequest_Temp.Value = Convert.ToDouble(Value);
            PushServiceClient_Temp.AddCurencyRate(CreateCurrencyRateRequest_Temp);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Timer_Tick();
            DateTime request = Convert.ToDateTime("06-08-2012");
            DateTime DateBegin_Temp = new DateTime(request.Year, request.Month, request.Day, 0, 0, 0);
            while (DateBegin_Temp.DayOfWeek != DayOfWeek.Sunday)
                DateBegin_Temp = DateBegin_Temp.AddDays(-1);
            DateTime DateEnd_Temp = DateBegin_Temp.AddDays(7);
        }

    }
}
